/* 
 * To change this template, choose Tools | Templates
 * 主要是一些常用的方法
 */

(function __Gbase__(){
    this.isMobile = true;
    /**
     * form表单相关的，主要是验证操作
     */
    this.from = {
        /**
         * 验证是否是邮箱
         */
        isMail : function(ele){
            return /[a-z0-9A-Z]+[a-zA-Z0-9\-\._]*@[a-z0-9A-Z]+[a-zA-Z0-9\-\._]?(\.[a-zA-Z]{2})/i.test($(ele).val());
        },
        /**
         * 检测两次输入的密码是否一致
         */
        checkPwd : function(ele1,ele2){
            return $(ele1).val() == $(ele2).val();
        },
        /**
         * 检测输入框内容是否已在最大长度之内
         */
        checkMaxLeng : function(ele,len){
            return $(ele).length <= len;
        },
        /**
         * 检测输入框内的字段是否在程满足最小长度
         */
        checkMinLen : function(ele,len){
            return $(ele).val().length >= len;
        },
        /**
         *初始化日期的下拉关联菜单         *
         **/
        initDropDateTime : function(y,m,d,defy,defm,defd){
            var ystart = 1950;            
            var yend = 2012;
            var y = $(y);
            var m = $(m);
            var d = $(d);
            var dy = 1980;
            var dm = 1;
            var dd = 1;
            if(defy){
                dy = parseInt(defy);
            }
            if(defm){
                dm = parseInt(defm);
            }
            if(defd){
                dd = parseInt(defd);
            }
            y.html("");
            for(;ystart <= yend ; ystart++){
                if(dy == ystart){
                    y.append("<option value='" + ystart +"' selected>" + ystart + "</option>");
                }else{
                    y.append("<option value='" + ystart +"'>" + ystart + "</option>");
                }
            }
            if(window.Gbase.isMobile){
                y.selectmenu('refresh');
            } 
            m.html("");
            
            for(var i = 1 ; i <= 12 ; i++){
                if(i == dm){
                    m.append("<option value='" + i +"' selected>" + Gbase.data.months[i] + "</option>");
                }else{
                    m.append("<option value='" + i +"'>" +  Gbase.data.months[i] + "</option>");
                }
            }
            if(window.Gbase.isMobile){
                m.selectmenu('refresh');
            }
            var endday = Gbase.data.monthdays[dm];
            if(dm == 2){
                if(dy % 400 == 0 || (dy % 100 != 0 && dy % 4 == 0)){
                    endday = 29;
                }
            }            
            for(var i = 1 ; i <= endday ; i++){
                if(dd == i){
                    d.append("<option value='" + i +"' selected>" + i + "</option>");
                }else{
                    d.append("<option value='" + i +"'>" + i + "</option>");
                }
            }
            if(window.Gbase.isMobile){
                d.selectmenu('refresh');
            }
            m.change(function(){
                Gbase.data.showMonth(parseInt(d.val()),parseInt(m.val()),d);
            });
            y.change(function(){
                Gbase.data.showMonth(parseInt(d.val()),parseInt(m.val()),d);
            })
        },
        /**
         *  初始省份和城市的下来菜单
         **/
        initDropCitys : function(p,c,list,defp,defc){
            var p = $(p);
            p.html('');
            for(var index in list){
                 var pp = list[index];
                 if(defp){
                     if(defp == pp.id){
                        p.append('<option value="' + pp.id+'" selected>' + pp.provinceName + '</option>'); 
                     }
                 }else{
                    p.append('<option value="' + pp.id+'">' + pp.provinceName + '</option>');
                 }
            }
            if(window.Gbase.isMobile){
                p.selectmenu('refresh');
            } 
            p.change(function(){
                var index = $(this).val();
                for(var i in list){
                    var pp = list[i];
                    if(pp.id == index){
                        c = $(c);
                        c.html("");
                        for(var ii in pp.citys){
                            var cc = pp.citys[ii];
                            c.append('<option value="'+cc.id+'">' + cc.cityName+ '</option>');
                        }
                        if(window.Gbase.isMobile){
                            c.selectmenu('refresh');
                        }                     
                    }
                } 
            });
            p.change();
            if(defc){
                c.children('option').each(function(){
                   if($(this).attr('value') == defc){
                       $(this).attr('selected','');
                        if(window.Gbase.isMobile){
                            c.selectmenu('refresh');
                        } 
                   } 
                });
            }
        }
    };
    /**
     * 消息相关的内容
     */
    this.msg = {
        inputError : function(){
            
        }
    };
    this.data = {
        months : {"1":"一月","2":"二月","3" : "三月" , "4" : "四月", "5" : "五月", "6" : "六月", "7" : "七月", "8" : "八月", "9" : "九月", "10" : "十月", "11" : "十一月", "12" : "十二月"},
        monthdays : {"1" : 31,"2" : 28,"3" : 31 ,"4" : 30,"5" : 31 ,"6" : 30 ,"7" : 31 ,"8" : 31 ,"9" : 30 , "10" : 31,"11" : 30 , "12" : 31},
        /**
         * 生成日期下拉菜单
         */
        showMonth : function(y,m,d){
            var endday = Gbase.data.monthdays[m];
            if(m == 2){
                if(y % 400 == 0 || (y % 100 != 0 && y % 4 == 0)){
                    m = 29;
                }
            }  
            d.html("");
            for(var i = 1 ; i <= endday ; i++){
                d.append("<option value='" + i +"'>" + i + "</option>");
            }
            if(window.Gbase.isMobile){
                d.selectmenu('refresh');
            }   
        }
    };
    window.Gbase = this;
})();

